<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * FreakAuth_light Class to handle the front controller
 * this class make code more reusable and it makes easier to
 * integrate Freakauth_light in your on templating system
 *
 * The class requires the use of
 *
 * => Database CI official library
 * => Db_session library (included in the download)
 * => FAL_validation library (included in the download)
 * => Freakauth_light library (included in the download)
 * => URL, FORM and FreakAuth_light (included in the download) helpers
 *
 * -----------------------------------------------------------------------------
 * Copyright (C) 2009 TechPundits (tech-pundits.com)
 * -----------------------------------------------------------------------------
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *------------------------------------------------------------------------------
 * @package     care and comfort 2009-09-18
 * @subpackage  Libraries
 * @category    Authentication
 * @author      pradeep kumar
 * @copyright   Copyright (c) 2009,TechPundits (tech-pundits.com)
 * @license		http://www.gnu.org/licenses/lgpl.html
 * @link 		http://tech-pundits.com
 * 
 *
 */

class CNC_frontend{
	
	function CNC_frontend(){
		$this->CI = &get_instance();
		//loads necessary libraries
        $this->CI->lang->load('freakauth');
        $this->CI->load->model('Contentmodel', 'content_model');
	    if ($this->CI->config->item('FAL_use_country'))
            $this->CI->load->model('country', 'country_model');
        //lets load the validation class if it hasn't been already loaded
        //it is needed by the FAL_validation library
        if (!class_exists('CI_Validation'))
		{
		     $this->CI->load->library('validation');
		}
		//let's load the FAL_validation library if it isn't already loaded
		if (!class_exists('FAL_validation'))
		{
		     $this->CI->load->library('FAL_validation');
		}
		
		//let's load the Freakauth_light library if it isn't already loaded
		//or autoloaded
		if (!class_exists('Freakauth_light'))
		{
		     $this->CI->load->library('Freakauth_light', 'freakauth_light');
		}
       
		//let's check if we have core classes extensions, and if we have them
		//let's load them
    	if ($this->CI->config->item('FAL_use_extensions'))
    	{
    	    $this->_loadExtensions();
    	}
    	else
    	{
    	    log_message('debug', 'FAL not using extensions');
    	}
       
		$this->CI->fal_validation->set_error_delimiters($this->CI->config->item('FAL_error_delimiter_open'), $this->CI->config->item('FAL_error_delimiter_close'));
       
	}
	 // --------------------------------------------------------------------
    /**
     * FAL extensions handler.
     * It makes easier to load extensions to the classes in 
     * (libraries/FreakAuth_light.php) or the FAL_validation.php class build
     *  your files according to the following specifications about file-names.
     *  You can place them either in system/libraries or in application/libraries
     * 
     * - libraries/MyFAL.php
     * - libraries/MyFALVal.php
     * 
     * Following this convention, if $config['FAL_use_extensions'] = TRUE;
     * the above files will be loaded 
     */
    function _loadExtensions()
    {
  
        if (file_exists(APPPATH.'libraries/MyFAL'.EXT) OR file_exists(BASEPATH.'libraries/MyFAL'.EXT))
        {
            //let's load the core library (i.e. FreakAuth_light.php) extension
            $this->CI->load->library('MyFAL');
            
            $this->CI->freakauth_light = new MyFAL();
            log_message('debug', 'MyFAL library loaded');
            log_message('debug', 'MyFAL class assigned to $this->CI->freakauth_light');
        }
        else
        {
            log_message('debug', 'MyFAL class not found');
        }

        if (file_exists(APPPATH.'libraries/MyFALVal'.EXT) OR file_exists(BASEPATH.'libraries/MyFALVal'.EXT))
        {
            //let's load the validation library (i.e. FAL_validation.php) extension
            //and assign it to $this->CI->fal_validation
            $this->CI->load->library('MyFALVal');
            $this->CI->fal_validation = new MyFALVal();

        }
        else
        {
            log_message('debug', 'MyFALVal class not found');
        }
    }
	
    // get the home page 
    function content_page($page_name)
    {
    	$data['heading'] = $this->CI->lang->line('CNC_'.$page_name.'_heading');
		$aContent = $this->CI->content_model->get_content($page_name);
    	$data['content'] = $aContent->page_content;
    	if($page_name=="AboutUs")
    	$data['signature'] = "Care and Comfort";
    	return $this->CI->load->view($this->CI->config->item('CNC_home_view'), $data, TRUE);
                //$this->CI->output->enable_profiler(TRUE);
    }
    
    // get services page 
    
    //get active side links
    function profile($user_id)
    {
    	
    	$this->CI->load->model('Sidebarmodel', 'sidebar_model');
    	 $this->CI->load->model('FreakAuth_light/usermodel', 'usermodel');
    	$user_profile_details = $this->CI->freakauth_light->_getUserProfile($user_id);
    	$query = $this->CI->usermodel->getUserById($user_id);
    	 $result = $query->row();
    	 $user_profile['email'] = $result->email;
    	 $user_profile['first_name'] = $user_profile_details['field_1'];
    	 $user_profile['last_name'] = $user_profile_details['field_2'];
    	 $user_profile['mobile_no'] = $user_profile_details['mobile_no'];
    	 $user_profile['land_line_no'] = $user_profile_details['land_line_no'];
    	 $user_profile['address'] = $user_profile_details['address'];
    	 $user_dob = $user_profile_details['dob'];
    	 $user_dob = explode('-',$user_dob);
    	 $user_profile['date'] = $user_dob[2];
    	 $user_profile['month'] = $user_dob[1];
    	 $user_profile['year'] = $user_dob[0];
    	 $user_profile['gender'] = $user_profile_details['gender'];
    	 //sets the necessary form fields
            $fields['first_name'] = $this->CI->lang->line('FAL_user_first_name_label');
            $fields['last_name'] = $this->CI->lang->line('FAL_user_last_name_label');
            $fields['mobile_no'] = $this->CI->lang->line('FAL_user_mobile_no_label');
            $fields['land_line_no'] = $this->CI->lang->line('FAL_user_land_line_no_label');
            $fields['address'] = $this->CI->lang->line('FAL_user_address_label');
//            $fields['user_name'] = $this->CI->lang->line('FAL_user_name_label');
//            $fields['password'] = $this->CI->lang->line('FAL_user_password_label');
//            $fields['password_confirm'] = $this->CI->lang->line('FAL_user_password_confirm_label');
            $fields['email'] = $this->CI->lang->line('FAL_user_email_label');
            $fields['date'] = $this->CI->lang->line('FAL_user_date_label');
            $fields['month'] = $this->CI->lang->line('FAL_user_month_label');
            $fields['year'] = $this->CI->lang->line('FAL_user_year_label');
            $fields['gender'] = $this->CI->lang->line('FAL_user_gender_label');
           
            //set validation rules
            	$rules['first_name'] = $this->CI->config->item('FAL_user_first_name_field_validation_register');
            $rules['last_name'] = $this->CI->config->item('FAL_user_last_name_field_validation_register');
            $rules['mobile_no'] = $this->CI->config->item('CNC_user_mobile_no_field_validation_register');
            $rules['land_line_no'] = $this->CI->config->item('CNC_user_land_line_no_field_validation_register');
            //$rules['email'] = "trim|required|valid_email|xss_clean";
            $rules['gender'] = $this->CI->lang->line('FAL_user_gender_field_validation_register');
       
        //do we also want to know the user country?
        if ($this->CI->config->item('FAL_use_country'))
        {
            $fields['country_id'] = $this->CI->lang->line('FAL_user_country_label');
            $rules['country_id'] = $this->CI->config->item('FAL_user_country_field_validation_register');
        }
        
       
        //-----------------------------------------------
        //ADD MORE FIELDS AND RULES HERE IF YOU NEED THEM
        //-----------------------------------------------
        
        $this->CI->fal_validation->set_fields($fields);
        $this->CI->fal_validation->set_rules($rules);
        if($this->CI->fal_validation->run())
        {
        	$values = $this->_getProfile_form();
        
        	$this->CI->load->model('Userprofile', 'userprofile_model');
        	$this->CI->userprofile_model->updateUserProfile($user_id,$values['user_profile']);
        	flashMsg('Profile details updated successfully');
   
            // redirects to homepage
            redirect('myaccount/profile', 'location');
        }
        else
        {
        	
        }
    	$data['menu'] = $this->CI->sidebar_model->get_active_sidebar();
    	//displays the view
	        $data['heading'] = $this->CI->lang->line('FAL_register_label');
	        
	        //date of bith options 

    		$date_options=array(); 
		 	      $date_options = array(''=>'Date');
		      for($i =1;$i<32;$i++)
			  {   
		           $date_options[$i] = $i;
			  }
	        	$year_options=array();
	        	$year_options[''] = $this->CI->lang->line('TG_user_year_label');
					$curreYear = date('Y');
					//$mydate = array('' => 'Year');
					for($i=$curreYear;$i>($curreYear-100);$i--)
						{
					$year_options[$i]=$i;
						}
	        						
		$month_options = array(''	=> $this->CI->lang->line('TG_user_month_label'),
	        						'01'	=> 'Jan',
	        						'02'	=> 'Feb',
	                                '03'    => 'Mar',
	        	                    '04'	=> 'Apr',
	        						'05'	=> 'May',
	                                '06'    => 'June',
	        	                    '07'	=> 'July',
	        						'08'	=> 'Aug',
	                                '09'   => 'Sep',
	        	                    '10'	=> 'Oct',
	        						'11'	=> 'Nov',
	                                '12'   => 'Dec',
	        						);
	        						
	        $data['date_options'] = $date_options;
	        $data['month_options'] = $month_options;
	        $data['year_options'] = $year_options;
	        $data['user_profile'] = $user_profile;
    	return $this->CI->load->view($this->CI->config->item('CNC_profile_view'), $data, TRUE);
    }
    
    function _getProfile_form()
    {
    	$values['user_profile']['field_1'] = $this->CI->input->post('first_name');
    	$values['user_profile']['field_2'] = $this->CI->input->post('last_name');
    	$values['user_profile']['mobile_no'] = $this->CI->input->post('mobile_no');
    	$values['user_profile']['land_line_no'] = $this->CI->input->post('land_line_no');
    	$values['user_profile']['address'] = $this->CI->input->post('address');
    	$values['user_profile']['dob'] = $this->CI->input->post('year').'-'.$this->CI->input->post('month').'-'.$this->CI->input->post('date');
//    	$values['user_profile']['month'] = $this->CI->input->post('month');
//    	$values['user_profile']['year'] = $this->CI->input->post('year');
    	$values['user_profile']['gender'] = $this->CI->input->post('gender'); 
    	return $values;
    }
    function get_active_sidebar()
    {
    	$this->CI->load->model('Sidebarmodel', 'sidebar_model');
    	$data['menu'] = $this->CI->sidebar_model->get_active_sidebar();
    	return $this->CI->load->view($this->CI->config->item('CNC_sidebar_view'), $data, TRUE);
    }
	function get_right_sidebar()
    {
    	//$this->CI->load->model('Sidebarmodel', 'sidebar_model');
    	//$data['menu'] = $this->CI->sidebar_model->get_active_sidebar();
    	$data['content'] = $this->CI->fal_front->login();
    	return $this->CI->load->view($this->CI->config->item('CNC_template_dir').'template/rt_sidebar', $data, TRUE);
    }
}